﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using System.ComponentModel;

namespace ViewMaker.Core.Controls
{
    /// <summary>
    /// チェックボックスViewControl
    /// </summary>
    public class CheckBoxViewControl : HeaderViewControl
    {
        /// <summary>
        /// プロパティ定数
        /// </summary>
        public new class Properties : HeaderViewControl.Properties
        {
            public const string Content = "Content";
            public const string IsThreeState = "IsThreeState";
            public const string CheckedUncheckedCommand = "CheckedUncheckedCommand";
        }

        /// <summary>
        /// コンテンツ
        /// </summary>
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "BindablePathList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string Content { get; set; }

        /// <summary>
        /// ３値形式のチェックボックス
        /// </summary>
        public bool? IsThreeState { get; set; }

        /// <summary>
        /// チェックON/OFF時に実行するコマンド
        /// </summary>
        [Display(GroupName = CommandGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "Item.CommandList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string CheckedUncheckedCommand { get; set; }


        [XmlIgnore]
        [Browsable(false)]
        public override bool CanOverrideWidth { get { return false; } }

        #region ShouldSerialize

        public virtual bool ShouldSerializeIsThreeState() { return IsThreeState.HasValue; }

        #endregion

        public CheckBoxViewControl()
        {
            ControlType = ViewControlType.CheckBox;

        }
        /// <summary>
        /// Xamlを生成する
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
            var list = base.CreateXaml(parent);
            var node = parent.CreateElement("CheckBox");
            SetBindingAttribute(node, "IsChecked", Item.BindPath, Item.IsOneWay);
            SetBindingAttribute(node, "Content", Content, true);

            if (IsThreeState.HasValue) node.SetAttribute("IsThreeState", IsThreeState.ToString());
            if (!string.IsNullOrEmpty(CheckedUncheckedCommand)) AddBehavior(node, "CheckedUncheckedBehavior", CheckedUncheckedCommand);
            ApplyBaseProperty(node);
            
            list.Add(node);
            return list;

        }
    }

}
